package cloneContainer;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class ButtonContainer extends JPanel implements CloneContainer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4980475397571637285L;
	JButton [] button;
	ButtonContainer() {
		button = new JButton[25];
		setLayout(new GridLayout(5, 5));
		for(int i = 0; i < 25; i++) {
			button[i] = new JButton();
			add(button[i]);
			button[i].addActionListener(this);
		}
	}
	
	@Override
	public Object cloneContainer() {
		// TODO Auto-generated method stub
		Object object = null;
		try {
			ByteArrayOutputStream outone = new ByteArrayOutputStream();
			ObjectOutputStream outtwo = new ObjectOutputStream(outone);
			outtwo.writeObject(this);
			ByteArrayInputStream inone = new ByteArrayInputStream(outone.toByteArray());
			ObjectInputStream intwo = new ObjectInputStream(inone);
			object = intwo.readObject();
		} catch(Exception event) {
			System.out.println(event);
		}
		return object;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton b = (JButton) e.getSource();
		Color newColor = JColorChooser.showDialog(null, "", b.getBackground());
		if(newColor != null)
			b.setBackground(newColor);
	}

}
